app.controller("ConsultantHomeController",['$location','$scope','$rootScope','userService','globalVariableService', 'snsService', '$modal',
    function($location,$scope,$rootScope,userService,globalVariableService, snsService, $modal){
        globalVariableService.initRootScope();
        $rootScope.showBackButton=false;
        $rootScope.showFooter=true;
        $rootScope.pageTitle="FashionSingles";
        $scope.isLoaded = false;
        $rootScope.homePageClick = true;
        $rootScope.searchPageClick = false;
        $rootScope.friendPageClick = false;
        $rootScope.snsPageClick = false;
        $rootScope.myselfPageClick = false;
        var imgSumCount = 0;
        var imgLoadedCount = 0;//图片加载完成数量
        var ind = 0;
        $scope.info = {};
        var images = [];
        userService.getRecommendMember().then(function(data){
            if(data.status == 0){
                images = data.data.recommend;
                for(var item=0; item<images.length; item++)
                {
                    var conIndex = images[item].constellation;
                    var edyIndex = images[item].education_background;
                    images[item].constellation = conste[conIndex].name;
                    images[item].education_background = education[edyIndex].name;
                }
                $('#consultant .content').text('正在加载...');
                $scope.info = images[ind];
                imgSumCount = images.length;
                for(var i=0;i<images.length;++i){
                    $('#consultant ul').append('<li  style="width:100%;height:100%"><img id="img' + (i + 1) + '" src="'+images[i].photo+'" style="width:100%;height:100%;"/></li>');
                    $scope.Imagess('img' + (i + 1), $scope.checkimg);
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });
        //判断是否加载完成
        $scope.Imagess = function (imgId, callback){
            var timerId = setInterval(function (){
                if($('#'+imgId).prop('complete')){
                    clearInterval(timerId);
                    callback();
                }
            }, 50);
        }

        //显示图片
        $scope.checkimg = function () {
            imgLoadedCount++;
            if(imgLoadedCount == imgSumCount) {
                $('#consultant').css('padding-top','0');
                $('#consultant ul').removeClass('ng-hide');
                $('#consultant div').remove();
                $scope.isLoaded = true;
                var slidey = $('#consultant').unslider({
                    dots: true,
                    keys: true,
                    speed:500,
                    fluid: true,
                    complete: function () {
                        ind = parseInt($('ol .active').text()) - 1;
                        $scope.info = images[ind];
                        $rootScope.$$phase || $rootScope.$apply();//让页面改变姓名年龄属性
                        $scope.$$phase || $scope.$apply();
                        //alert($scope.info.name);
                    }
                });
                var unslider = slidey.data('unslider');
                var slides = jQuery('.banner');
                slides.on('swipeleft', function(e) {
                    unslider.next();
                }).on('swiperight', function(e) {
                    unslider.prev();
                });
            }
        }


        $scope.imgClick =function(){
            var toPath = "/detailsInfo";
            if(!globalVariableService.checkLogin(toPath)){
                return;
            }else{
                userService.getProfile().then(function(data){
                    if(data.status == 0) {
                        if (data.data.user_status == 0) {
                            $location.path('/promptRegister');
                        }
                        if (data.data.user_status == 1) {
                           /* modalMessage($modal, '您的资料还在审核中,请耐心等待');*/
                            $location.path("/promptExamine").search({backUrl: '/'});
                        }
                        if (data.data.user_status == 2) {
                            /*modalMessage($modal, '您的资料已通过审核');*/
                            globalVariableService.userId = images[ind].userId;
                            $location.path("/detailsInfo");
                        }
                        if (data.data.user_status == 3) {
                            /*modalMessage($modal, '您的资料没有通过审核，原因是有所填内容不符合,请重新注册');
                            $location.path("/loadingHead");*/
                            $location.path("/promptFeedback");
                        }
                    }else{
                        var modalInstance = $modal.open({
                            templateUrl: 'views/tmpl_modal_alert.html',
                            controller: ModalAlertCtrl,
                            size: 'sm',
                            resolve: {
                                alertObj: function () {
                                    return {message: data.friendlyMessage};
                                }
                            }
                        });
                    }
                });
            }
        }
}]);
